/**
 * ①添
 * @@新增 定义初始化菜单
 */
import store from '@/store'
import {
  getToken,
  localSave,
  localRead,
  localRemove
} from '@/libs/util'
import {
  lazyLoadingCop
} from '@/libs/tools'
import {
  getMockMenuData
} from '@/api/data'
import Main from '@/components/main' // Main 是架构组件，不在后台返回，在文件里单独引入
import parentView from '@/components/parent-view' // parentView 是二级架构组件，不在后台返回，在文件里单独引入
const _import = require('@/router/_import_' + process.env.NODE_ENV) // 获取组件的方法

// 初始化路由
export const initRouter = (vm) => {
	if (!getToken()) {
	  return
	}
	return new Promise((resolve, reject) => {
		var gotRouter = dynamicRouterAdd()
		if (gotRouter) {
			store.commit('updateMenuList', gotRouter)
		} else {
			getMockMenuData().then(res => {
				var routerData = res.data // 后台拿到路由
				localSave('dynamicRouter', JSON.stringify(routerData)) // 存储路由到localStorage
				gotRouter = filterAsyncRouter(routerData) // 过滤路由,路由组件转换
				store.commit('updateMenuList', gotRouter)
			})
		}
		resolve(gotRouter)
	})
}

// 加载路由菜单,从localStorage拿到路由,在创建路由时使用
export const dynamicRouterAdd = () => {
  let dynamicRouter = []
  let data = localRead('dynamicRouter')
  if (data) {
    dynamicRouter = filterAsyncRouter(JSON.parse(data))
    return dynamicRouter
  } else {
    return ''
  }
}

// @函数: 遍历后台传来的路由字符串，转换为组件对象
export const filterAsyncRouter = (asyncRouterMap, tp, path) => {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (route.path) {
			let rtp = path || route.path
			let comp = lazyLoadingCop(rtp + '/' + route.path)
			let comt = lazyLoadingCop(route.path + '/' + route.path)
      let { name, icon, hideInBread = '', hideInMenu = '' } = route
      if (name) {
        route.meta = { title: name, icon, hideInBread, hideInMenu }
      }
			route.name = route.path
      if (route.children && route.children.length) {
        let rtp = route.path
        if (tp > 0) { rtp = path + '/' + route.path }
        route.children = filterAsyncRouter(route.children, tp + 1 || 0, rtp)
      } else {
        route.children = []
      }
			let obj = {}
			let cobj = {}
			let meta = JSON.parse(JSON.stringify(route.meta));
			cobj.meta = meta
			cobj.meta.hideInMenu = true
			cobj.path = route.path
			cobj.name = route.path
			cobj.component = comp
			cobj.children= []
			obj.meta = meta
			obj.meta.hideInMenu = true
			obj.name = route.path
			if(tp == undefined){
				route.component = Main
				route.redirect = '/' + route.path + '/' + route.path
        route.path = '/' + route.path
				obj.path = '/' + route.path
				obj.component = Main
				obj.children = [cobj]
				route.children.push(obj)
			}else if(tp == 0){
				if(!route.children.length){
					obj.path = route.path
					obj.component = comt
					obj.children = [];
					route.children.push(obj)
				}
				route.component = Main
				route.path = '/' + route.path
			}else if(tp == 1){
				if (route.children.length) {
				  route.component = parentView
				} else {
				  if (comp) {
				    route.component = comp
				  } else {
				    return false
				  }
				}
			}else if(tp == 2){
				if (comp) {
				  route.component = comp
				} else {
				  return false
				}
			}
    } else {
      return false
    }
    return true
  })
  return accessedRouters
}
